/*
# Copyright (c) 2024 Huawei Device Co., Ltd.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
*/
#ifndef OH_WEB_RTC_OHOS_CODEC_DATA_H
#define OH_WEB_RTC_OHOS_CODEC_DATA_H

#include <mutex>
#include <queue>

#include "ohos_video_common.h"

namespace webrtc {
namespace ohos {
class CodecData {
public:
  ~CodecData();
  FormatInfo *formatInfo {nullptr};
  std::atomic<bool> isRunning_ {false};
  void Start();
  void ShutDown();
  class DataCallback {
  public:
    static void OnCodecError(OH_AVCodec *codec, int32_t errorCode, void *userData);
    static void OnCodecFormatChange(OH_AVCodec *codec, OH_AVFormat *format, void *userData);
    static void OnNeedInputBuffer(OH_AVCodec *codec, uint32_t index, OH_AVBuffer *buffer, void *userData);
    static void OnNewOutputBuffer(OH_AVCodec *codec, uint32_t index, OH_AVBuffer *buffer, void *userData);
  };
  uint32_t inputFrameCount_ {0};
  uint32_t outputFrameCount_ {0};
  int32_t InputData(CodecBufferInfo &info, std::chrono::milliseconds time);
  int32_t OutputData(CodecBufferInfo &info);
private:
  std::mutex inputMutex_;
  std::condition_variable inputCond_;
  std::queue<CodecBufferInfo> inputBufferInfoQueue_;
  std::mutex outputMutex_;
  std::condition_variable outputCond_;
  std::queue<CodecBufferInfo> outputBufferInfoQueue_;
};

}
}

#endif //OH_WEB_RTC_OHOS_CODEC_DATA_H
